import random
import string
while True:
    try:
        n = input("请输入要生成的激活码的个数：")
        n = int(n)
        fi = open('key.txt', 'a+', encoding='utf-8')
        for i in range(n):
            req_key = ''.join(random.sample(string.ascii_uppercase + string.digits, 32))+'\n'  # 随机生成32位由A-Z 0-9组成的数字
            fi.write(req_key)
            fi.close()
        print("激活码写入成功，已保存！")
        break
    except ValueError as can:
        print("输入类型有误，请重新输入",can)


